//
//  VRVistorView.swift
//  Sina_MicoBlog
//
//  Created by Lee on 16/5/11.
//  Copyright © 2016年 Lee.C. All rights reserved.
//
//上面的basecontroller已经被继承了,而且他的view已经换成了这个类的了,所以我们现在设置这个类里面的东西,在我的思路中

    //*****懒加载创建
    //我觉得需要先懒加载需要的图片还有textlabel还有按钮
    //先做第一幅图的,因为第一幅图是最复杂的,然后到时候第二个tabbar的时候再隐藏就可以了
    //因为第一幅图有3个图片,然后一个label,然后一个两个按钮,所以需要懒加载6个.
    //*****创建方法.
    //写init(frame)方法,是因为在创建()的时候就会调用这个方法
    //约束跟添加都放到这里了.
    //******然后先在init方法中先添加控件,然后再设置他的约束
        //约束,拉开那个coverView,也就是平铺那个coverView使他变大
        //他的约束是按钮的底部是这个coverView的底部

    //****然后修改了他的背景颜色了
    //****然后设置这个轮的动画,然后写成一个公开方法,到时候回到控制器,判断是不是这个类直接调用.
    //*****然后写一个公开的方法,来设置图片,还有内容了.

    //到base控制器里面利用方法来设置内容setVIstorInfo来做
    //****在base控制器回来这边,然后回来,进行创建代理,声明代理属性,然后声明代理方法
            //代理属性是需要在class里面声明的,然后代理属性要用weak修饰来创建


import UIKit

  @objc  protocol VRVistorViewdelegate: NSObjectProtocol{

        func visitorViewDidClickRegister()
        func visitorViewDidClickLogin()
    

}



class VRVistorView: UIView {

    weak var delegate: VRVistorViewdelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVistorInfo (imageName: String,textInfo: String){
        //设置这个图片了
        iconView.image = UIImage(named: imageName)
        //设置这个文字了
        massageLabel.text = textInfo
        
        //然后隐藏小屋
        homeView.hidden = true
        //隐藏遮盖的那个鬼东西
        coverView.hidden = true
   
    }
    
    
    
    //MARK: - 设置这个轮的动画
    func starRotationAnimation (){
    let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        //当画面换一个controller的时候,动画会消失,然后我们就需要设置这个不消失
        animation.removedOnCompletion = false
        //这个核心动画要加载这个layer层上面
        iconView.layer.addAnimation(animation, forKey: nil)
  
    }
    
    
    
    //MARK: - 添加控件并设置控件的约束
    private func prepareUI (){
    
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(massageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //先添加完才能设置约束
        //****在添加约束前,是需要取消自动布局的!!!
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        massageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //开始约束
        //iconView的约束
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //透明纸的约束//这里是
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //先是小屋的约束
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //信息label的约束 这里设置nil的时候,他就是宽是多少
        self.addConstraint(NSLayoutConstraint(item: massageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        self.addConstraint(NSLayoutConstraint(item: massageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: massageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //注册label的约束
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: massageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: massageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录label的约束
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: massageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: massageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
    
    }
    
    //MARK: - 懒加载
//懒加载是私有的.懒加载私有本文件可以访问属性,但是私有的方法本文件是不能访问的
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //有一个遮盖的需要懒加载
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子的view
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //三个图片都懒加载完以后,然后又label
    //不能直接赋值的话,就用{}闭包模式设置这个massage的属性
    //闭包式懒加载
    //后面是需要加载的,表示创建,因为懒加载也要创建一次
    private lazy var massageLabel: UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人,看看有上面惊喜!"
        label.textColor = UIColor.blackColor()
        label.numberOfLines = 0
        label.sizeToFit()
        
    return label
    }()
    
    //创建注册按钮
    private lazy var registerButton:UIButton = {
    
        let button:UIButton = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //添加绑定事件
        button.addTarget(self, action: "registerIn", forControlEvents: UIControlEvents.TouchUpInside)
    
        return button
    }()
    
    //创建登录按钮
    private lazy var loginButton: UIButton = {
    
        let button:UIButton = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "loginIn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    
    }()
    //MARK: - 按钮点击事件实现方法
  @objc private  func registerIn (){
    delegate?.visitorViewDidClickRegister()
    
    }
    
  @objc private  func loginIn () {
    
    delegate?.visitorViewDidClickLogin()
    
    }
}
